package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.behavior.service.ApArticleRelationService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnLikeBehaviorService;
import com.heima.model.behavior.dtos.ApArticleRelationDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class ApArticleRelationServiceImpl implements ApArticleRelationService {

    @Resource
    ApBehaviorEntryService apBehaviorEntryService;

    @Resource
    ApLikesBehaviorService apLikesBehaviorService;

    @Resource
    ApUnLikeBehaviorService apUnLikeBehaviorService;

    @Override
    public Map findApArticleRelation(ApArticleRelationDto dto) {
        HashMap resultMap = new HashMap();
        resultMap.put("islike",false);
        resultMap.put("isunlike",false);
        if(dto==null||dto.getArticleId()==null||dto.getEntryId()==null){
            return resultMap;
        }

        ApBehaviorEntry apBehaviorEntry = null;

        if(dto.getType()==1){
           apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getEntryId(),null);
        }else{
           apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(null,dto.getEntryId());
        }

        if(apBehaviorEntry==null){
            return resultMap;
        }

        resultMap.put("entryId",apBehaviorEntry.getId());

        ApLikesBehavior apLikesBehavior = apLikesBehaviorService.getOne(Wrappers.<ApLikesBehavior>lambdaQuery()
                .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
        );

        if(apLikesBehavior!=null&&apLikesBehavior.getOperation()==0){
            resultMap.put("islike",true);
        }

        ApUnlikesBehavior apUnlikesBehavior  = apUnLikeBehaviorService.getOne(Wrappers.<ApUnlikesBehavior>lambdaQuery()
                .eq(ApUnlikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApUnlikesBehavior::getEntryId, apBehaviorEntry.getId())
        );

        if(apUnlikesBehavior!=null&&apUnlikesBehavior.getType().intValue()==0){
            resultMap.put("isunlike",true);
        }
        return resultMap;
    }
}